package com.correction.servlet;

import java.io.IOException;

import javax.servlet.ServletException;

import com.correction.classes.Exercice;
import com.correction.classes.Exercice.Langage;
import com.correction.classes.NoteExercice;

public class Correction {

	public static NoteExercice correct(Exercice exercice, UploadService uploadService)
			throws ServletException, IOException {
		int total = 0;
		NoteExercice note = new NoteExercice();
		String result = "";

		// verification
		String content = uploadService.getContent();
		boolean isUsedOk = checkUsed(content, exercice.getToUse());
		System.out.println("isUsedOk: " + isUsedOk);
		boolean isNotUsedOk = checkNotUsed(content, exercice.getToNotUse());
		System.out.println("isNotUsedOk: " + isNotUsedOk);
		// execution avec parm
		if (exercice.getLangage().equals(Langage.C)) {
			System.out.println("Langage C >");
			result = uploadAndExecuteC(uploadService, exercice);
		} else if (exercice.getLangage().equals(Langage.JAVA)) {
			System.out.println("Langage Java >");
			result = uploadAndExecuteJava(uploadService, exercice);
		}
		String expectedResult = exercice.getResultat();
		boolean isResultOk = checkResult(expectedResult, result);
		System.out.println("isResultOk: " + isResultOk);
		if (isUsedOk && isNotUsedOk && isResultOk) {
			note.setNote(exercice.getNombrePoint());
		} else
			note.setNote(0);
		note.setExercice(exercice);
		return note;
	}

	private static boolean checkResult(String expectedResult, String result) {
		System.out.println("expectedResult : " + expectedResult);
		System.out.println("result :" + result);
		if (expectedResult.equalsIgnoreCase(result))
			return true;
		return false;
	}

	private static boolean checkNotUsed(String content, String toNotUse) {
		if (toNotUse.contains(";")) {
			String[] toNotUseArray = toNotUse.split(";");
			for (int i = 0; i < toNotUseArray.length; i++) {
				if (content.contains(toNotUseArray[i]))
					return false;
			}
		}
		return true;
	}

	private static boolean checkUsed(String content, String toUse) {
		if (toUse.contains(";")) {
			String[] toUseArray = toUse.split(";");
			System.out.println("toUseArray.lenght : " + toUseArray.length);
			for (int i = 0; i < toUseArray.length; i++) {
				if (!content.contains(toUseArray[i]))
					return false;
			}
		}
		return true;
	}

	private static String uploadAndExecuteJava(UploadService uploadService,
			Exercice exercice) {
		/* Compile and execute */
		String result = "";
		ExecuteLangageJAVA executeLangageJAVA = new ExecuteLangageJAVA();
		result = executeLangageJAVA.executeWithoutDirection(
				uploadService.getPath(), uploadService.getFileName(),
				executeLangageJAVA.getCommand(), exercice);
		return result;
	}

	private static String uploadAndExecuteC(UploadService uploadService,
			Exercice exercice) {
		/* Compile and execute */
		String result = "";
		ExecuteLangageC executeLangageC = new ExecuteLangageC();
		System.out.println("uploadAndExecuteC GO");
		result = executeLangageC.executeWithoutDirection(
				uploadService.getPath(), uploadService.getFileName(),
				executeLangageC.getCommand(), exercice);
		return result;
	}

}